package com.zlkj.pro.modular.business.service.impl;

import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlkj.pro.core.util.ConvertUtil;
import com.zlkj.pro.modular.business.entity.ProductTypeEntity;
import com.zlkj.pro.modular.business.mapper.ProductTypeMapper;
import com.zlkj.pro.modular.business.model.dto.ProductTypeDTO;
import com.zlkj.pro.modular.business.model.vo.ProductTypeVO;
import com.zlkj.pro.modular.business.service.ProductTypeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author liyang
 * @date 2024/4/6
 * @time 14:34
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductTypeEntity> implements ProductTypeService {

    @Override
    public PageResult<ProductTypeVO> findPage(ProductTypeDTO dto) {
        Integer pageNo = dto.getPageNo();
        Integer pageSize = dto.getPageSize();
        LambdaQueryWrapper<ProductTypeEntity> queryWrapper = Wrappers.lambdaQuery();
        // 名称模糊搜索
        queryWrapper.like(StringUtils.isNotBlank(dto.getName()), ProductTypeEntity::getName, dto.getName());
        // 添加排序条件
        if (StringUtils.isNotBlank(dto.getSort()) && StringUtils.isNotBlank(dto.getOrder())) {
            if (dto.getSort().equalsIgnoreCase("dataSort")) {
                queryWrapper.orderBy(true, dto.getOrder().equalsIgnoreCase("asc"), ProductTypeEntity::getDataSort);
            }
        } else {
            queryWrapper.orderByAsc(ProductTypeEntity::getDataSort);
        }
        Page<ProductTypeEntity> productTypeEntityPage = super.page(new Page<>(pageNo, pageSize), queryWrapper);
        long total = productTypeEntityPage.getTotal();
        List<ProductTypeEntity> records = productTypeEntityPage.getRecords();
        List<ProductTypeVO> productTypeAdminList = ConvertUtil.turn(records, ProductTypeVO.class);
        return PageResultFactory.createPageResult(productTypeAdminList, total, pageSize, pageNo);
    }

    @Override
    public List<ProductTypeVO> findList(ProductTypeDTO dto) {
        LambdaQueryWrapper<ProductTypeEntity> productTypeEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        productTypeEntityLambdaQueryWrapper.orderByDesc(ProductTypeEntity::getCreateTime);
        List<ProductTypeEntity> productTypeEntityList = super.list(productTypeEntityLambdaQueryWrapper);
        return ConvertUtil.turn(productTypeEntityList, ProductTypeVO.class);
    }

    @Override
    public ProductTypeVO findDetail(ProductTypeDTO dto) {
        Long id = dto.getId();
        LambdaQueryWrapper<ProductTypeEntity> productTypeEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        productTypeEntityLambdaQueryWrapper.eq(ProductTypeEntity::getId, id);
        ProductTypeEntity productTypeEntity = super.getOne(productTypeEntityLambdaQueryWrapper, Boolean.FALSE);
        return ConvertUtil.turn(productTypeEntity, ProductTypeVO.class);
    }

    @Override
    public boolean add(ProductTypeDTO dto) {
        ProductTypeEntity productTypeEntity = ConvertUtil.turn(dto, ProductTypeEntity.class);
        productTypeEntity.setUpdateTime(new Date());
        return super.save(productTypeEntity);
    }

    @Override
    public boolean edit(ProductTypeDTO dto) {
        ProductTypeEntity productTypeEntity = ConvertUtil.turn(dto, ProductTypeEntity.class);
        return super.updateById(productTypeEntity);
    }

    @Override
    public boolean delete(ProductTypeDTO dto) {
        return super.removeById(dto);
    }

    @Override
    public boolean batchDelete(List<Long> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            return super.removeByIds(ids);
        }
        return true;
    }

}
